<template>
  <div>
    <div v-if="isPageShow" class="wrap">
      <!-- header头部 -->
      <!-- <m-header
        :activeIndex="3"
        :backcolor="'#1968FA'"
        :color="'#fff'"
        opacity="1"
        hocolor="#fff"
        filter="blur(0px)"
      ></m-header> -->
      <div style="height: 50px"></div>
      <div class="container" style="width: 100%; max-width: 1200px">
        <!-- nav部分 -->
        <div class="nav-box container" style="width: 100%; max-width: 1200px">
          <div class="name nav-flex">
            <div class="title">名称：</div>
            <div class="btn">
              <el-button
                size="mini"
                round
                style="font-size: 16px"
                @click="handleAllOption('name')"
                :type="isNameSelect == '' ? 'primary' : ''"
                plain
                >全部</el-button
              >
            </div>
            <div class="option">
              <div
                v-for="(item, index) in titleList"
                :key="index"
                @click="handleCheckOption('name', item)"
                :class="isNameSelect == item ? 'op-item active' : 'op-item'"
              >
                {{ item }}
              </div>
            </div>
          </div>
          <!-- <div class="type nav-flex">
            <div class="title">类别：</div>
            <div class="btn">
              <el-button
                size="mini"
                round
                style="font-size: 16px"
                @click="handleAllOption('type')"
                :type="isTpyeSelect == '' ? 'primary' : ''"
                plain
                >全部</el-button
              >
            </div>
            <div class="option">
              <div
                v-for="(item, index) in eventTypeList"
                :key="item"
                @click="handleCheckOption('type', item, index)"
                :class="isTpyeSelect == item ? 'op-item active' : 'op-item'"
              >
                {{ item }}
              </div>
            </div>
          </div>
          <div class="time nav-flex">
            <div class="title">比赛时间：</div>
            <div class="btn">
              <el-button
                size="mini"
                round
                @click="handleAllOption('time')"
                :type="isTimeSelect == '' ? 'primary' : ''"
                plain
                style="font-size: 16px"
                >全部</el-button
              >
            </div>
            <div class="option">
              <div
                v-for="item in createTimeList"
                :key="item"
                @click="handleCheckOption('time', item)"
                :class="isTimeSelect == item ? 'op-item active' : 'op-item'"
              >
                {{ item }}
              </div>
            </div>
          </div> -->
        </div>
        <!-- content内容部分 -->
        <div class="course-wrap">
          <div class="container" style="width: 100%; max-width: 1200px">
            <el-row :gutter="40" v-loading="$store.state.isLoading">
              <el-col
                class="search-wrap"
                :sm="6"
                :xs="24"
                v-for="item in dataList"
                :key="item.id"
              >
                <div class="detail-box" @click="handleGoUrl(item)">
                  <div class="box-img">
                    <el-image
                      lazy
                      style="width: 100%; height: 100%"
                      :src="item.img != '' ? item.img : morImg"
                    ></el-image>
                  </div>
                  <div class="box-content">
                    <div class="title" v-text="item.eventName"></div>
                    <div class="sign">
                      <span class="pe-num">{{ item.numStus }}人学习</span>
                      <span class="date">{{
                        item.createTime ? item.createTime : "暂无时间"
                      }}</span>
                    </div>
                  </div>
                </div>
              </el-col>
              <el-empty
                description="暂无数据"
                v-if="dataList.length < 1"
              ></el-empty>
            </el-row>
          </div>
          <div class="clearfix" v-if="totalCount > 8">
            <el-pagination
              class="pull-right"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              :background="true"
              layout="total,prev,pager,next,sizes,jumper"
              :total="totalCount"
            ></el-pagination>
          </div>
        </div>
      </div>
      <!-- footer页脚 -->
      <!-- <m-fotter /> -->
    </div>
  </div>
</template>

<script>
import morImg from "../../assets/images/xbook.png";
import { STATUS_CODE_OK } from "@/config";
import {
  getMatchResource,
  getMatchRequestParam,
} from "@/api/eventResources.js";
import { permissionVerification } from "@/api/homePage.js";
import state from "@/store/state";
// import MHeader from "@/components/MHeader.vue";
// import MFotter from "@/components/MFotter.vue";
export default {
  name: "EventResources",
  data() {
    return {
      morImg,
      isPageShow: false, //页面是否显示
      isNameSelect: "", // 名称筛选时选中
      isTpyeSelect: "", // 类别筛选时选中
      isTimeSelect: "", // 时间筛选时选中
      eventName: "", //赛项名称
      type: "", //赛项类别
      time: "", //比赛时间
      dataList: [],
      createTimeList: [], // 比赛时间
      eventTypeList: [], // 赛项类别
      titleList: [], //赛项名称
      currentPage: 1, // 页数
      pageSize: 20, // 条数
      totalCount: 0, // 总条数
    };
  },
  created() {
    if (this.$route.query.row) {
      this.handleCheckOption("name", this.$route.query.row);
    }
    this.handleGetMatchResource(true);
    this.handleGetMatchRequestParam();
  },
  methods: {
    async hanldePermissionVerification(item) {
      let { data } = await permissionVerification({
        allocationId: item.id,
      });
      if (data.code == STATUS_CODE_OK) {
        if (data.success) {
          this.$router.push({
            name: "oneXbook",
            params: { row: item },
          });
        } else {
          this.$notify.warning({
            title: "提示",
            message: "暂无浏览权限,请联系管理员",
          });
          return;
        }
      }
    },
    // 跳转
    handleGoUrl(item) {
      if (!state.userData.token) {
        this.$router.push({
          path: "/login",
        });
      } else {
        this.$router.push({
          name: "oneXbook",
          params: { row: item },
        });
        // this.hanldePermissionVerification(item);
      }
    },
    // 筛选全部
    handleAllOption(valueType) {
      if (valueType == "name") {
        this.isNameSelect = "";
        this.eventName = "";
      }
      if (valueType == "type") {
        this.isTpyeSelect = "";
        this.type = "";
      }
      if (valueType == "time") {
        this.isTimeSelect = "";
        this.time = "";
      }
      this.handleGetMatchResource(true);
    },
    // 筛选单个选项
    handleCheckOption(valueType, item, index = "") {
      if (valueType == "name") {
        this.isNameSelect = item;
        this.eventName = item;
      }
      if (valueType == "type") {
        this.isTpyeSelect = item;
        this.type = index;
      }
      if (valueType == "time") {
        this.isTimeSelect = item;
        this.time = item;
      }
      this.handleGetMatchResource(true);
    },
    // 获取筛选参数
    async handleGetMatchRequestParam() {
      let { data } = await getMatchRequestParam({
        eveMoId: "1775402520624824321",
      });
      if (data.code == STATUS_CODE_OK) {
        this.createTimeList = data.data.createTimeList;
        this.eventTypeList = data.data.eventTypeList;
        this.titleList = data.data.titleList;
        this.isPageShow = true;
      }
    },
    // 获取资源页接口数据
    async handleGetMatchResource(isReFresh = false) {
      // this.eventName = this.$route.query.row;
      if (isReFresh) {
        this.currentPage = 1;
      }
      let { data } = await getMatchResource({
        // 模版id
        eveMoId: "1775402520624824321",
        eventName: this.eventName,
        grade: "",
        type: this.type,
        time: this.time,
        startPage: this.currentPage,
        endPage: this.pageSize,
      });
      if (data.code == STATUS_CODE_OK) {
        this.totalCount = Number(data.data.total);
        this.dataList = data.data.records;
        this.dataList.forEach((item) => {
          if (item.createTime) {
            item.createTime = item.createTime.split(" ")[0];
          }
        });
      }
    },
    // 分页页数改变事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.handleGetMatchResource(true);
    },
    // 分页当前页改变事件
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handleGetMatchResource();
    },
  },
  // components: {
  //   MHeader,
  //   MFotter,
  // },
};
</script>

<style scoped lang="scss">
$bg-color: #fff;
.active {
  color: #409eff;
}
::v-deep .el-row {
  min-height: 500px;
}
.content-overflow-scroll {
  height: 600px;
  overflow: auto;
  li {
    height: 50px;
    line-height: 50px;
    margin-bottom: 10px;
    border: 1px solid #ececec;
    text-align: center;
  }
}
.wrap {
  position: relative;
  background-color: $bg-color;
  .container {
    .banner-wrap {
      position: relative;
      height: 400px;
      display: flex;
      align-items: center;
      margin: 30px;
      .banner {
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        // object-fit: cover;
        z-index: 1;
      }
    }
    .nav-box {
      margin: 30px 0px 0px 0px;
      padding: 20px;
      background: #fff;
      color: #333;
      font-size: 16px;
      border-radius: 10px 10px 10px 10px;
      .nav-flex {
        display: flex;
        padding: 10px 0px;
        .title {
          flex: 0.08;
          text-align: center;
          line-height: 40px;
        }
        .btn {
          flex: 0.03;
          padding-top: 8px;
        }
        .option {
          display: flex;
          flex-wrap: wrap;
          flex: 0.89;
          text-align: center;
          .op-item {
            margin: 0px 10px;
            // min-width: 100px;
            min-height: 44px;
            line-height: 44px;
            cursor: pointer;
          }
        }
      }
    }
  }

  .course-wrap {
    padding: 4px 0 40px 0;
    .container {
      .search-wrap {
        padding-top: 6px;
        .detail-box {
          background: #fff;
          box-shadow: 0px 0px 6px 1px rgba(0, 0, 0, 0.04);
          border-radius: 10px 10px 10px 10px;
          margin-top: 10px;
          .box-img {
            width: 100%;
            height: 150px;
            padding: 10px;
          }
          .box-content {
            box-sizing: border-box;
            padding: 10px;
            .title {
              padding: 10px 0px;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
            }
            .sign {
              display: flex;
              align-items: center;
              justify-content: space-between;
              .pe-num {
                color: #ff9500;
              }
            }
          }
        }
      }
    }
  }
}
</style>
